package cn.edu.seu.ssny.service.impl;

import cn.edu.seu.ssny.dao.IEquipmentDao;
import cn.edu.seu.ssny.dao.support.IBaseDao;
import cn.edu.seu.ssny.entity.*;
import cn.edu.seu.ssny.service.IDeviceService;
import cn.edu.seu.ssny.service.IEquipmentService;
import cn.edu.seu.ssny.service.IEquipmentTypeInProjectService;
import cn.edu.seu.ssny.service.IEquipmentTypeService;
import cn.edu.seu.ssny.service.support.impl.BaseServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.tags.form.OptionsTag;

import java.util.*;

/**
 * 设备表
 */
@Service
public class EquipmentServiceImpl extends BaseServiceImpl<Equipment, Integer> implements IEquipmentService {

    @Autowired
    private IEquipmentDao equipmentDao;

    @Autowired
    private IEquipmentTypeService equipmentTypeService;

    @Autowired
    private IEquipmentTypeInProjectService equipmentTypeInProjectService;

    @Override
    public IBaseDao<Equipment, Integer> getBaseDao() {
        return this.equipmentDao;
    }

    @Override
    public List<Equipment> findByProject(Project project) {
        return equipmentDao.findByProject(project);
    }

    @Override
    public List<Equipment> findEquipmentsByProjectId(Integer projectId) {
        return equipmentDao.findEquipmentsByProjectId(projectId);
    }

    @Override
    public List<Equipment> findByEnterprise(Enterprise enterprise) {
        return equipmentDao.findByEnterprise(enterprise);
    }

    @Override
    public Optional<Equipment> findByName(String name) {
        return equipmentDao.findByName(name);
    }

    @Override
    public void saveOrUpdate(Equipment equipment, Enterprise enterprise, Project project, EquipmentType type) {

        if (equipment.getId() != null) {
            Equipment dbEquipment = find(equipment.getId());
            dbEquipment.setName(equipment.getName());
            dbEquipment.setType(equipment.getType());
            dbEquipment.setModelNum(equipment.getModelNum());
            dbEquipment.setImage(equipment.getImage());
            dbEquipment.setOpenTime(equipment.getOpenTime());
            dbEquipment.setMaintainTime(equipment.getMaintainTime());
            dbEquipment.setManager(equipment.getManager());
            dbEquipment.setSysClass(equipment.getSysClass());
            if (dbEquipment.getSysClass() == null) {
                dbEquipment.setSysType(null);
            } else {
                dbEquipment.setSysType(dbEquipment.getSysClass().getClassName());
            }
            if (type != null) {
                // 每次都需要进行更新，因为传入的是equipmentType
                System.out.println("*********" + type.getName());
                Optional<EquipmentTypeInProject> typeInProject = equipmentTypeInProjectService.findByProjectAndTypeName(
                        project, type);
                if (typeInProject.isPresent()) {
                    System.out.println("*********" + typeInProject.get().getTypeName().getName());
                    dbEquipment.setType(typeInProject.get());
                } else {
                    equipmentTypeInProjectService.create(project, type);
                    typeInProject = equipmentTypeInProjectService.findByProjectAndTypeName(
                            project, type);
                }
                assert typeInProject.isPresent();
                dbEquipment.setType(typeInProject.get());
            }
            update(dbEquipment);
        } else {
            equipment.setDisplay(false);
            equipment.setIsMaintain(false);
            equipment.setProject(project);
            equipment.setEnterprise(enterprise);
            if (equipment.getSysClass() == null) {
                equipment.setSysType(null);
            } else {
                equipment.setSysType(equipment.getSysClass().getClassName());
            }
            if (type != null) {
                // 每次都需要进行更新，因为传入的是equipmentType
                System.out.println("*********" + type.getName());
                Optional<EquipmentTypeInProject> typeInProject = equipmentTypeInProjectService.findByProjectAndTypeName(
                        equipment.getProject(), type);
                if (typeInProject.isPresent()) {
                    System.out.println("*********" + typeInProject.get().getTypeName().getName());
                    equipment.setType(typeInProject.get());
                } else {
                    equipmentTypeInProjectService.create(equipment.getProject(), type);
                    typeInProject = equipmentTypeInProjectService.findByProjectAndTypeName(
                            equipment.getProject(), type);
                }
                assert typeInProject.isPresent();
                equipment.setType(typeInProject.get());
            }
            save(equipment);
        }
    }

    @Override
    public void saveCoordinate(Equipment equipment) {
        Equipment dbEquipment = find(equipment.getId());
        dbEquipment.setX1(equipment.getX1());
        dbEquipment.setX2(equipment.getX2());
        dbEquipment.setY1(equipment.getY1());
        dbEquipment.setY2(equipment.getY2());
        dbEquipment.setDisplay(equipment.getDisplay());
        update(dbEquipment);
    }

    @Override
    public void setMaintainTime(Equipment equipment) {
        Equipment dbEquipment = find(equipment.getId());
        dbEquipment.setMaintainTime(equipment.getMaintainTime());
        update(dbEquipment);
    }

    @Override
    public void maintain(Integer equipmentId) {
        Equipment dbEquipment = find(equipmentId);
        dbEquipment.setLastMaintainTime(new Date());
        dbEquipment.setIsMaintain(true);
        update(dbEquipment);
    }


    @Override
    public Device getEquipmentDeviceByType(Equipment equipment, String deviceType) {
        List<Device> deviceList = equipment.getDevices();
        for (Device device : deviceList) {
            if (device.getDescription().equals(deviceType)) {
                return device;
            }
        }
        return null;

    }

    @Override
    public List<Equipment> findBySysClassId(Long sysClassId) {
        return equipmentDao.findBySysClassId(sysClassId);
    }

}